#!/bin/bash
# 需要/usr/local/src/目录有redis-6.0.6.tar.gz这个文件

# 升级gcc版本
yum -y install gcc make tcl				
yum -y install centos-release-scl			
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
source /etc/profile

# 安装redis
yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel 
cd /usr/local/src/
tar -zxf redis-6.0.6.tar.gz
cd redis-6.0.6/
make && make install 
echo $?

# 添加主配置文件，初始化redis
mkdir /etc/redis/
mkdir /var/lib/redis/
sed -i "/^bind/ s/^/#/" ./redis.conf
sed -i '/^protected-mode/ s/yes/no/' ./redis.conf
sed -i '/^logfile/ s#""# "/var/log/redis.log"#' ./redis.conf
sed -i '/^dir/ s#.*#dir /var/lib/redis/#' ./redis.conf

# 开启集群配置
#sed -i '/cluster-enabled/ s/# //' ./redis.conf
#sed -i '/cluster-config-file/ s/# //' ./redis.conf
#sed -i '/cluster-node-timeout/ s/# //' ./redis.conf

# 拷贝主配置文件
cp ./redis.conf /etc/redis/

# 添加systemd文件
cat > /usr/lib/systemd/system/redis.service<<-EOF
[Unit]
Description=Redis
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecReload=/usr/local/bin/redis-server -s reload
ExecStop=/usr/local/bin/redis-server -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
	
# 启动redis
systemctl daemon-reload
systemctl enable redis
systemctl restart redis
systemctl status redis
ss -antl  | grep 6379